import { useMemo } from 'react';
import { deepClone } from '~/public/cpas-business/tools/Tools';

const formatNum = (cellInfo: any) => {
  const value = cellInfo?.value;
  if (value && +value !== 0) {
    return value.toString();
  } else {
    return '';
  }
};
export function useProviderDataHook(
  dates: string[],
  type: 'year' | 'moon' | 'moonDetail' | 'yearDetail',
) {
  /**
   * 表头数据
   */
  const tableColumnData = useMemo(() => generateTableColumnData(dates, type), [dates, type]);
  function generateTableColumnData(datesParam: string[], typeParam: string): any[] {
    const sortDatesParam = datesParam.sort((a: any, b: any) => a - b);

    // 基础信息列
    const columnBaseData = [
      {
        caption: '供应商',
        dataField: 'supplierName',
        alignment: 'left',
        minWidth: 120,
        dataTypeCustom: 'zh-string',
        fixed: true,
        // width: 'auto',
      },
      {
        caption: '供应商类别',
        dataField: 'typeName',
        width: 120,
        dataTypeCustom: 'zh-string',
      },
      {
        caption: '是否关联方',
        dataField: 'isRelate',
        width: 120,
        dataType: 'string',
        customizeText: (cellInfo: any) => {
          if (cellInfo.value === null) {
            return '';
          } else if (cellInfo.value === true) {
            return '是';
          } else {
            return '否';
          }
        },
      },
    ];
    // 年度统计列
    const yearColumn = {
      caption: '本期期末',
      alignment: 'center',
      columns: [
        {
          caption: '数量',
          dataField: 'bqqc_cwbbs',
          dataType: 'number',
          // dataTypeCustom: 'money-number',
          width: 120,
          alignment: 'right',
          customizeText: formatNum,
        },
        {
          caption: '金额',
          dataField: 'bqqm_wss',
          dataType: 'number',
          dataTypeCustom: 'money-number',
          width: 120,
          alignment: 'right',
        },
        {
          caption: '占比',
          dataField: 'bqqm_cy',
          dataType: 'number',
          dataTypeCustom: 'money-number',
          width: 80,
          alignment: 'right',
          customizeText: (cellInfo: any) => {
            if (+cellInfo.value === 0) {
              return '';
            } else if (cellInfo.value > 0 && (cellInfo.value * 100).toFixed(2) === '0.00') {
              return '';
            } else {
              return (cellInfo.value * 100).toFixed(2) + '%';
            }
          },
        },
        {
          caption: '排名',
          dataField: 'bqqm_cy',
          dataType: 'number',
          // dataTypeCustom: 'money-number',
          width: 80,
          alignment: 'right',
          customizeText: formatNum,
        },
      ],
    };
    // 月度统计列
    const moonColumn = {
      caption: '月',
      alignment: 'center',
      columns: [
        {
          caption: '数量',
          dataField: 'bqqc_cwbbs',
          dataType: 'number',
          // dataTypeCustom: 'money-number',
          alignment: 'right',
          width: 120,
          customizeText: formatNum,
        },
        {
          caption: '金额',
          dataField: 'bqqm_wss',
          dataType: 'number',
          dataTypeCustom: 'money-number',
          alignment: 'right',
          width: 120,
        },
      ],
    };

    const countColumn = [
      {
        caption: '总计',
        alignment: 'center',
        columns: [
          {
            caption: '数量',
            dataField: 'Qty',
            width: 120,
            dataType: 'number',
            // dataTypeCustom: 'money-number',
            alignment: 'right',
            customizeText: formatNum,
          },
          {
            caption: '金额',
            dataField: 'Money',
            width: 120,
            dataType: 'number',
            dataTypeCustom: 'money-number',
            alignment: 'right',
          },
          {
            caption: '占比',
            dataField: 'Rate',
            width: 80,
            dataType: 'number',
            dataTypeCustom: 'money-number',
            alignment: 'right',
            customizeText: (cellInfo: any) => {
              if (+cellInfo.value === 0) {
                return '';
              } else if (cellInfo.value > 0 && (cellInfo.value * 100).toFixed(2) === '0.00') {
                return '';
              } else {
                return (cellInfo.value * 100).toFixed(2) + '%';
              }
            },
          },
          {
            caption: '排名',
            dataField: 'OrderIndex',
            width: 80,
            dataType: 'number',
            alignment: 'right',
            customizeText: formatNum,
          },
        ],
      },
    ];

    const dynamicColumns: any[] = [];

    sortDatesParam.forEach((dateItem) => {
      if (typeParam === 'year') {
        const newYearColumn: any = deepClone(yearColumn);

        newYearColumn.caption = dateItem + '年';
        // 数量
        newYearColumn.columns[0].dataField = `${dateItem}_Qty`;
        // 金额
        newYearColumn.columns[1].dataField = `${dateItem}_Money`;
        // 占比
        newYearColumn.columns[2].dataField = `${dateItem}_Rate`;
        // 排名
        newYearColumn.columns[3].dataField = `${dateItem}_OrderIndex`;

        dynamicColumns.push(newYearColumn);
      } else if (typeParam === 'moon') {
        const newMoonColumn: any = deepClone(moonColumn);
        newMoonColumn.caption = dateItem + '月';
        // 数量
        newMoonColumn.columns[0].dataField = `${dateItem}_Qty`;
        // 金额
        newMoonColumn.columns[1].dataField = `${dateItem}_Money`;
        dynamicColumns.push(newMoonColumn);
      }
    });
    if (typeParam === 'moon') {
      return [...columnBaseData, ...dynamicColumns, ...countColumn];
    } else {
      return [...columnBaseData, ...dynamicColumns];
    }
  }

  return {
    tableColumnData,
  };
}

export function useProviderGoodsDataHook(dates: string[], type: 'year' | 'moon') {
  /**
   * 表头数据
   */
  const tableColumnData = useMemo(() => generateTableColumnData(dates, type), [dates, type]);
  function generateTableColumnData(datesParam: string[], typeParam: string): any[] {
    const sortDatesParam = datesParam.sort((a: any, b: any) => a - b);

    // 基础信息列
    const columnBaseData = [
      {
        caption: '供应商',
        dataField: 'supplierName',
        alignment: 'left',
        minWidth: 120,
        dataTypeCustom: 'zh-string',
        fixed: true,
        // width: 'auto',
      },
      {
        caption: '商品名称',
        dataField: 'productName',
        minWidth: 120,
        dataTypeCustom: 'zh-string',
        fixed: true,
        // width: 'auto',
      },
      {
        caption: '供应商类别',
        dataField: 'typeName',
        width: 120,
      },
      {
        caption: '商品类别',
        dataField: 'productType',
        width: 120,
        dataTypeCustom: 'zh-string',
      },
      {
        caption: '商品编码',
        dataField: 'productCode',
        alignment: 'left',
        width: 120,
        dataTypeCustom: 'zh-string',
      },
      {
        caption: '基本计量单位',
        dataField: 'baseUnitCode',
        width: 120,
        dataTypeCustom: 'zh-string',
      },
      {
        caption: '是否关联方',
        dataField: 'isRelate',
        width: 120,
        dataType: 'string',
        customizeText: (cellInfo: any) => {
          if (cellInfo.value === null) {
            return '';
          } else if (cellInfo.value === true) {
            return '是';
          } else {
            return '否';
          }
        },
      },
    ];
    const moonBaseData = [
      {
        caption: '供应商',
        dataField: 'supplierName',
        alignment: 'left',
        minWidth: 120,
        dataTypeCustom: 'zh-string',
        fixed: true,
        // width: 'auto',
      },
      {
        caption: '商品名称',
        dataField: 'productName',
        minWidth: 120,
        dataTypeCustom: 'zh-string',
        fixed: true,
        // width: 'auto',
      },
      {
        caption: '供应商类别',
        dataField: 'typeName',
        width: 120,
        dataTypeCustom: 'zh-string',
      },
      {
        caption: '商品类别',
        dataField: 'productType',
        width: 120,
        dataTypeCustom: 'zh-string',
      },
      {
        caption: '商品编码',
        dataField: 'productCode',
        alignment: 'left',
        width: 120,
      },
      {
        caption: '基本计量单位',
        dataField: 'baseUnitCode',
        width: 120,
        dataTypeCustom: 'zh-string',
      },
      {
        caption: '是否关联方',
        dataField: 'isRelate',
        width: 120,
        dataType: 'string',
        customizeText: (cellInfo: any) => {
          if (cellInfo.value === null) {
            return '';
          } else if (cellInfo.value === true) {
            return '是';
          } else {
            return '否';
          }
        },
      },
    ];
    // 年度统计列
    const yearColumn = {
      caption: '本期期末',
      alignment: 'center',
      columns: [
        {
          caption: '数量',
          dataField: 'bqqc_cwbbs',
          dataType: 'number',
          // dataTypeCustom: 'money-number',
          width: 120,
          alignment: 'right',
          customizeText: formatNum,
        },
        {
          caption: '金额',
          dataField: 'bqqm_wss',
          dataType: 'number',
          dataTypeCustom: 'money-number',
          width: 120,
          alignment: 'right',
        },
        {
          caption: '均价',
          dataField: 'bqqm_cy',
          dataType: 'number',
          dataTypeCustom: 'money-number',
          width: 120,
          alignment: 'right',
        },
      ],
    };
    // 月度统计列
    const moonColumn = {
      caption: '月',
      alignment: 'center',
      columns: [
        {
          caption: '数量',
          dataField: 'bqqc_cwbbs',
          dataType: 'number',
          // dataTypeCustom: 'money-number',
          alignment: 'right',
          width: 120,
          customizeText: formatNum,
        },
        {
          caption: '均价',
          dataField: 'cost',
          width: 120,
          dataType: 'number',
          dataTypeCustom: 'money-number',
          alignment: 'right',
        },
        {
          caption: '金额',
          dataField: 'bqqm_wss',
          dataType: 'number',
          dataTypeCustom: 'money-number',
          alignment: 'right',
          width: 120,
        },
      ],
    };

    const countColumn = [
      {
        caption: '总计',
        alignment: 'center',
        columns: [
          {
            caption: '数量',
            dataField: 'Qty',
            width: 120,
            dataType: 'number',
            // dataTypeCustom: 'money-number',
            alignment: 'right',
            customizeText: formatNum,
          },
          {
            caption: '均价',
            dataField: 'Avg',
            width: 120,
            dataType: 'number',
            dataTypeCustom: 'money-number',
            alignment: 'right',
          },
          {
            caption: '金额',
            dataField: 'Money',
            width: 120,
            dataType: 'number',
            dataTypeCustom: 'money-number',
            alignment: 'right',
          },
        ],
      },
    ];
    const dynamicColumns: any[] = [];

    sortDatesParam.forEach((dateItem) => {
      if (typeParam === 'year') {
        const newYearColumn: any = deepClone(yearColumn);

        newYearColumn.caption = dateItem + '年';
        // 数量
        newYearColumn.columns[0].dataField = `${dateItem}_Qty`;
        // 金额
        newYearColumn.columns[1].dataField = `${dateItem}_Money`;
        // 占比
        newYearColumn.columns[2].dataField = `${dateItem}_Avg`;

        dynamicColumns.push(newYearColumn);
      } else if (typeParam === 'moon') {
        const newMoonColumn: any = deepClone(moonColumn);
        newMoonColumn.caption = dateItem + '月';
        // 数量
        newMoonColumn.columns[0].dataField = `${dateItem}_Qty`;
        // 单价
        newMoonColumn.columns[1].dataField = `${dateItem}_Avg`;
        // 金额
        newMoonColumn.columns[2].dataField = `${dateItem}_Money`;

        dynamicColumns.push(newMoonColumn);
      }
    });
    if (typeParam === 'moon') {
      return [...moonBaseData, ...dynamicColumns, ...countColumn];
    } else {
      return [...columnBaseData, ...dynamicColumns];
    }
  }

  return {
    tableColumnData,
  };
}
